<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="模块名称">
              <a-input placeholder="" v-model="queryParam.moduleName"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="日志类型">
              <a-select v-model="queryParam.logType">
                <a-select-option value="0">正常</a-select-option>
                <a-select-option value="1">异常</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.init()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      :scroll="{ x: 3000 }"
      :columns="columns"
      :dataSource="tableData"
      :loading="loading"
      :pagination="pagination"
      :rowKey="record => record.id"
      @change="handleTableChange"
    >
    </a-table>
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@api/sys/log'
import { Ellipsis } from '@/components'
const statusList = [
  {
    label: '正常',
    value: '0'
  },
  {
    label: '异常',
    value: '1'
  }
]
export default {
  name: 'User',
  components: {
    Ellipsis
  },
  data () {
    return {
      loading: false,
      columns: [
        {
          title: '#',
          width: '100px',
          fixed: 'left',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        },
        {
          title: '日志类型',
          fixed: 'left',
          width: '100px',
          dataIndex: 'logType',
          customRender: (text, record, index) => {
            return (
              <div>
                {
                  statusList.map(item => {
                    if (item.value === text) {
                      return item.label
                    }
                  })
                }
              </div>
            )
          }
        },
        {
          title: '用户名',
          width: '100',
          dataIndex: 'username'
        }, {
          title: '用户操作',
          width: '150px',
          dataIndex: 'operation'
        },
        {
          title: '请求方法',
          dataIndex: 'method'
        },
        {
          title: '请求参数',
          dataIndex: 'params',
          customRender: (text, record, index) => {
            return (
              <div>
                <ellipsis length={50} tooltip>
                  {text}
                </ellipsis>
              </div>
            )
          }
        },
        {
          title: '执行时长(毫秒)',
          dataIndex: 'time'
        },
        {
          title: '模块名称',
          width: '10%',
          dataIndex: 'moduleName'
        },
        {
          title: '来源ip',
          width: '10%',
          dataIndex: 'ip'
        },
        {
          title: '创建时间',
          width: '10%',
          dataIndex: 'createTime'
        }, {
          title: '请求方法',
          width: '10%',
          dataIndex: 'requestMethod'
        },
        {
          title: '浏览器',
          width: '10%',
          dataIndex: 'userAgent'
        },
        {
          title: '异常',
          width: '10%',
          dataIndex: 'exception'
        },
        {
          title: '操作',
          width: '200px',
          fixed: 'right',
          customRender: (text, record, index) => {
            return (
              <div>
                <a-popconfirm title="是否删除日志" onConfirm={(e) => this.delete(record)} okText="是" cancelText="否">
                  <a-icon slot="icon" type="question-circle-o" style="color: red"/>
                  <a href="javascript:;" style="color: red">删除</a>
                </a-popconfirm>
              </div>
            )
          }
        }
      ],
      tableData: [],
      pagination: {
        pageSize: 10,
        current: 1,
        total: 0,
        showSizeChanger: true
      },
      pages: {
        size: 10,
        current: 1
      },
      queryParam: {
        moduleName: '',
        logType: ''
      }
    }
  },
  methods: {
    init () {
      this.loading = true
      fetchList({
        ...this.queryParam,
        ...this.pages
      }).then(res => {
        this.tableData = res.data.records
        this.pagination.total = res.data.total
        this.loading = false
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = pagination
      this.pages.size = pagination.pageSize
      this.pages.current = pagination.current
      this.init()
    },
    delete (record) {
      delObj(record.id).then(res => {
        this.$message.info('删除成功')
        this.init()
      })
    },
    show (record) {
      this.$refs.userAddOrUpdate.edit(record.userId, 'show')
    },
    handleOk () {
      this.init()
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
